/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * infoPanel.java
 *
 * Created on Jul 13, 2009, 10:25:11 PM
 */

package coda.gui;

import coda.util.Media;
import org.bff.javampd.objects.MPDSong;

/**
 *
 * @author pan
 */
public class InfoPanel extends javax.swing.JPanel {

    private MPDSong song = null;
    private String album = null;
    private String artist = null;
    private String title = null;

    /** Creates new form infoPanel */
    public InfoPanel() {
        initComponents();
    }

    public void setSong(MPDSong song) {
        this.song = song;
        update();
    }

    private void update() {
        String newAlbum;
        String newArtist;
        String newTitle;
        //
        if (song != null) {
            newAlbum = song.getAlbum().getName();
            newArtist = song.getArtist().getName();
            newTitle = song.getTitle();
        } else {
            newAlbum = "";
            newArtist = "";
            newTitle = "";
        }
        //
        if (!newAlbum.equals(album)) {
            album = newAlbum;
            albumLabel.setText(album);
            updateCover();
        }
        if (!newArtist.equals(artist)) {
            artist = newArtist;
            artistLabel.setText(artist);
        }
        if (!newTitle.equals(title)) {
            title = newTitle;
            titleLabel.setText(title);
            updateLyrics();
        }
    }

    private void updateCover() {
        String ar = (song != null) ? song.getArtist().getName() : null;
        String al = (song != null) ? song.getAlbum().getName() : null;
        label.setIcon(Media.getCover(ar, al));
    }

    private void updateLyrics() {
        if (song != null) {
            lyrics.setText(Media.getLyrics(song.getArtist().getName(), song.getTitle()));
        } else {
            lyrics.setText(null);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        label = new javax.swing.JLabel();
        artistLabel = new javax.swing.JLabel();
        albumLabel = new javax.swing.JLabel();
        titleLabel = new javax.swing.JLabel();
        lyricsPane = new javax.swing.JScrollPane();
        lyrics = new javax.swing.JTextArea();
        editButton = new javax.swing.JToggleButton();
        getButton = new javax.swing.JButton();

        setLayout(new java.awt.GridBagLayout());

        label.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/media.png"))); // NOI18N
        label.setMaximumSize(new java.awt.Dimension(100, 100));
        label.setMinimumSize(new java.awt.Dimension(100, 100));
        label.setPreferredSize(new java.awt.Dimension(100, 100));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints.insets = new java.awt.Insets(8, 8, 8, 4);
        add(label, gridBagConstraints);

        artistLabel.setFont(new java.awt.Font("Verdana", 1, 13));
        artistLabel.setForeground(new java.awt.Color(164, 223, 125));
        artistLabel.setText("Artist");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(8, 0, 2, 8);
        add(artistLabel, gridBagConstraints);

        albumLabel.setFont(new java.awt.Font("Verdana", 3, 12));
        albumLabel.setText("Album");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 0, 2, 8);
        add(albumLabel, gridBagConstraints);

        titleLabel.setFont(new java.awt.Font("Verdana", 0, 12));
        titleLabel.setText("Title");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 0, 2, 8);
        add(titleLabel, gridBagConstraints);

        lyricsPane.setBorder(null);
        lyricsPane.setMinimumSize(new java.awt.Dimension(280, 280));
        lyricsPane.setPreferredSize(new java.awt.Dimension(360, 280));

        lyrics.setColumns(20);
        lyrics.setFont(new java.awt.Font("Verdana", 0, 10));
        lyrics.setRows(5);

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, editButton, org.jdesktop.beansbinding.ELProperty.create("${selected}"), lyrics, org.jdesktop.beansbinding.BeanProperty.create("editable"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, editButton, org.jdesktop.beansbinding.ELProperty.create("${selected}"), lyrics, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        lyricsPane.setViewportView(lyrics);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 8, 8, 8);
        add(lyricsPane, gridBagConstraints);

        editButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/edit.png"))); // NOI18N
        editButton.setFocusPainted(false);
        editButton.setMaximumSize(new java.awt.Dimension(24, 24));
        editButton.setMinimumSize(new java.awt.Dimension(24, 24));
        editButton.setPreferredSize(new java.awt.Dimension(24, 24));
        editButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(8, 4, 8, 4);
        add(editButton, gridBagConstraints);

        getButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/get.png"))); // NOI18N
        getButton.setFocusPainted(false);
        getButton.setMaximumSize(new java.awt.Dimension(24, 24));
        getButton.setMinimumSize(new java.awt.Dimension(24, 24));
        getButton.setPreferredSize(new java.awt.Dimension(24, 24));
        getButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                getButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridheight = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
        gridBagConstraints.insets = new java.awt.Insets(8, 0, 8, 8);
        add(getButton, gridBagConstraints);

        bindingGroup.bind();
    }// </editor-fold>//GEN-END:initComponents

    private void editButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editButtonActionPerformed
        if (!editButton.isSelected()) {
            if (song != null) {
                Media.setLyrics(song.getArtist().getName(), song.getTitle(), lyrics.getText());
            }
        }
    }//GEN-LAST:event_editButtonActionPerformed

    private void getButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_getButtonActionPerformed
        if (song != null) {
            lyrics.setText(Media.fetchLyrics(song.getArtist().getName(), song.getTitle()));
        }
    }//GEN-LAST:event_getButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel albumLabel;
    private javax.swing.JLabel artistLabel;
    private javax.swing.JToggleButton editButton;
    private javax.swing.JButton getButton;
    private javax.swing.JLabel label;
    private javax.swing.JTextArea lyrics;
    private javax.swing.JScrollPane lyricsPane;
    private javax.swing.JLabel titleLabel;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

}
